package TruongNhaLib;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Color;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.Log;

public class SubjectMark {
	private int size_full = MarkModel.size_full; // = 28
	private DecimalFormat format = new DecimalFormat("#.#");	
	private boolean newMark = false;
	private String title;
	private int status = 0;
	private int statusNotNotifyYet = 0;
	private int id;
	private String[] Diem28;
	private JSONArray arr;
	private SpannableString[] spanChilds = new SpannableString[4];
	private String[] childs = new String[]{"Đ miệng:","15 phút:","45 phút:","Cuối kỳ:"};
	public SubjectMark(JSONObject jsonObj){
		//Log.d("constructor", "call");
		Diem28 = new String[size_full];
		Arrays.fill(Diem28, "");
		try {
			this.title = jsonObj.getString("name");
			this.id = jsonObj.getInt("id");
			arr = jsonObj.getJSONArray("mark");
			int size = arr.length();
			for(int i = 0 ; i< size ; i++ ){
				JSONObject obj = arr.getJSONObject(i);
				Diem28[obj.getInt("n") - 1] = obj.getString("m");
				int j = (obj.getInt("n") - 1)/8 ;
				//Log.d("tag", j +"");
				if(j<=3 && j>=0){
					childs[j] = childs[j] + String.format(" (%3s) ", obj.getString("m"));
				}
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//parser json
		for (int i = 0; i < 4; i++) {
			spanChilds[i] = new SpannableString(childs[i]);
			spanChilds[i].setSpan(new ForegroundColorSpan(Color.RED), 0, 8, 0);  
		}
		//Log.d("constructor", "done");
	}
	public JSONArray getJsonArr(){
		return arr;
	}
	public int getId(){
		return this.id;
	}
	public String[] getDiem28(){
		return Diem28;
	}
	public void setStatus(int i){
		this.status = 0;
	}
	public int getStatus(){
		return this.status;
	}
	public String getTitle(){
		return title;
	}
	public void setTitle(String title){
		this.title = title ;
	}
	public int getChildNumber(){
		return 4;
	}
	public String getChilds(int i){
		return childs[i] ;
	}
	public SpannableString getSpanChilds(int i){
		return spanChilds[i];
	}
	public JSONArray getArr(){
		return arr;
	}
	public boolean haveNew(){
		return newMark;
	}
	//true- giong nhau
	public void compare(SubjectMark old){
		//Log.d(arr.length()+" = ", old.arr.length()+" ") ;
		int delta = arr.length() - old.arr.length();
		if(delta >0){
			statusNotNotifyYet = delta;
			status = status + delta;
			//Log.d("status + ", delta+"");
		}
	}
	public void copyStatus(SubjectMark old){
		status = old.status;
	}
	public List<String> getNotifyString(SubjectMark old){
		List<String> result = new ArrayList<String>();
		if(statusNotNotifyYet<=0) return result;
		String[] Diem28Old = old.getDiem28();
		//Filter dif
		Log.d("testing", "start test");
		for(int i = 0 ; i< size_full; i++){
			Log.d("testing", "loop "+i);
			if(!Diem28[i].equals(Diem28Old[i])){
				Log.d("testing", "diff");
				String s = "";
				int j = (i)/8 ;
				switch (j) {
				case 0:
					s = s + " Điểm miệng: ";
					break;
				case 1:
					s = s + " Điểm 15 phút: ";
					break;
				case 2:
					s = s + " Điểm 45 phút: ";					
					break;
				case 3:
					s = s + " Điểm cuối kỳ: ";
					break;
				}
				s = s + String.format(" (%s) ", Diem28[i]);
				result.add(s);
			}
		}//end for
		Log.d("testing", "end test");
		return result;
	}
}//end class
